<?php
class Mmaster extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    function auto_asset($q){
      $q = strtolower($q);
      $query = "
        SELECT id_asset, name, kode
        FROM m_asset
        WHERE
        LOWER(name) LIKE '%{$q}%' OR
        LOWER(kode) LIKE '%{$q}%'
        LIMIT 0, 12
        ";
//print $query;
      $data = $this->db->query($query)->result();
      $items = array();
      if(is_array($data)){
        foreach($data as $dt){
          $items[$dt->name." ".$dt->kode] = $dt->id_asset;
        }
      }
      return $items;
    }
    function auto_asset_management($id_gudang, $q){
      $q = strtolower($q);
      $query = "
        SELECT A.id_asset, A.name, A.kode, sum(B.qty) AS qty
        FROM m_asset AS A
        JOIN d_asset_record_items_gudang AS B ON A.id_asset = B.id_asset
        WHERE
        (LOWER(A.name) LIKE '%{$q}%' OR
        LOWER(A.kode) LIKE '%{$q}%') AND
        B.status = 2 AND
        B.id_asset_gudang = {$id_gudang} AND
        qty > 0
        GROUP BY B.id_asset
        LIMIT 0, 12
        ";
//print $query;
      $data = $this->db->query($query)->result();
      $items = array();
      if(count($data) > 0){
        foreach($data as $dt){
          $items[$dt->name." ".$dt->kode] = $dt->id_asset;
        }
      }
      else{
        $items = array(
            'Pilih Gudang Terlebih Dahulu atau' => 0,
            'Tidak Ada Barang di Gudang atau' => 0,
            'Keyword Anda Tidak Ada yang Cocok' => 0
        );
      }
      return $items;
    }
    function auto_gudang($q){
      $q = strtolower($q);
      $query = "
        SELECT id_asset_gudang, name
        FROM m_asset_gudang
        WHERE
        LOWER(name) LIKE '%{$q}%'
        LIMIT 0, 12
        ";
//print $query;
      $data = $this->db->query($query)->result();
      $items = array();
      if(is_array($data)){
        foreach($data as $dt){
          $items[$dt->name] = $dt->id_asset_gudang;
        }
      }
      return $items;
    }
    function auto_supplier($q){
      $q = strtolower($q);
      $query = "
        SELECT id_supplier, title
        FROM m_supplier
        WHERE
        LOWER(title) LIKE '%{$q}%'
        LIMIT 0, 12
        ";
//print $query;
      $data = $this->db->query($query)->result();
      $items = array();
      if(is_array($data)){
        foreach($data as $dt){
          $items[$dt->title] = $dt->id_supplier;
        }
      }
      return $items;
    }
    function get($order,$table, $where = array(), $field_not = "nbs", $not_in = array(), $select = "*"){
    if($field_not != "nbs" AND $not_in)
      $this->db->where_not_in($field_not, $not_in);
    if($where)
      $this->db->where($where);
    $this->db->select($select);
    $this->db->order_by($order, "ASC"); 
    $data = $this->db->get($table)->result();
    if(is_array($data))
      return $data;
    else
      return array();
  }
  
    function insert_pendapatan_outlet($id_outlet, $kirim){
      $relasi = $this->global_models->get_array("id_pendapatan_lain", "d_pendapatan_lain_outlet", 
              array("id_outlet" => $id_outlet));
      $array_del = array_diff($relasi, $kirim);
      if(is_array($array_del)){
        foreach($array_del as $ad){
          $this->global_models->delete("d_pendapatan_lain_outlet", array("id_outlet" => $id_outlet, "id_pendapatan_lain" => $ad));
        }
      }
      if(is_array($kirim)){
        foreach($kirim as $krm){
          $cek = $this->global_models->get_field("d_pendapatan_lain_outlet", "id_outlet", 
                  array("id_outlet"  =>  $id_outlet, "id_pendapatan_lain" => $krm));
          if($cek === FALSE){
            $this->global_models->insert("d_pendapatan_lain_outlet", 
                    array(
                        "id_outlet" => $id_outlet, 
                        "id_pendapatan_lain" => $krm,
                        "create_by_users" =>  $this->session->userdata('id'),
                        "create_date" =>  date("Y-m-d H:i:s"),
                        "update_by_users" =>  $this->session->userdata('id'),
                        ));
          }
          else{
            $this->global_models->update("d_pendapatan_lain_outlet", 
                    array(
                        "id_outlet" =>  $id_outlet,
                        "id_pendapatan_lain"  =>  $krm
                    ),
                    array(
                        "update_by_users" =>  $this->session->userdata('id'),
                        ));
          }
        }
        return true;
      }
      return false;
    }
    function get_data_export(){
      $this->db->select('m_asset.*, m_asset_kategori.name as name_kategori, m_asset_brand.name as name_brand');
      $this->db->join('m_asset_kategori', 'm_asset.id_asset_kategori = m_asset_kategori.id_asset_kategori', 'left');
      $this->db->join('m_asset_brand', 'm_asset.id_asset_brand = m_asset_brand.id_asset_brand', 'left');
      $this->db->order_by("m_asset.id_asset", "DESC");
      $data = $this->db->get("m_asset")->result(); 
      return $data;
    }
    
    function export_xls($filename){
      $objPHPExcel = $this->phpexcel;
      $objPHPExcel->getProperties()->setCreator("Mr Montir")
							 ->setLastModifiedBy("Mr Montir")
							 ->setTitle("Asset Data")
							 ->setSubject("Asset Data")
							 ->setDescription("Report Asset data.")
							 ->setKeywords("report Asset data")
							 ->setCategory("Asset");

      $objPHPExcel->setActiveSheetIndex(0);
      
      $objPHPExcel->getActiveSheet()->mergeCells('A1:I2');
      $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Data Asset');
      $objPHPExcel->getActiveSheet()->getStyle('A1:I2')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
      $objPHPExcel->getActiveSheet()->getStyle('A1:I2')->getFill()->getStartColor()->setARGB('FF808080');
      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
      $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
      $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
      
      $objPHPExcel->getActiveSheet()->setCellValue('A4', 'Name');
      $objPHPExcel->getActiveSheet()->setCellValue('B4', 'Kategori');
      $objPHPExcel->getActiveSheet()->setCellValue('C4', 'Brand');
      $objPHPExcel->getActiveSheet()->setCellValue('D4', 'Kode');
      $objPHPExcel->getActiveSheet()->setCellValue('E4', 'Panjang');
      $objPHPExcel->getActiveSheet()->setCellValue('F4', 'Lebar');
      $objPHPExcel->getActiveSheet()->setCellValue('G4', 'Tinggi');
      $objPHPExcel->getActiveSheet()->setCellValue('H4', 'Massa');
      $objPHPExcel->getActiveSheet()->setCellValue('I4', 'Note');
      $objPHPExcel->getActiveSheet()->getStyle('A4:I4')->applyFromArray(
          array(
            'font'    => array(
              'bold'      => true
            ),
            'alignment' => array(
              'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
            ),
            'borders' => array(
              'top'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
              'bottom'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
              'left'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
              'right'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
            ),
            'fill' => array(
              'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
                'rotation'   => 90,
              'startcolor' => array(
                'argb' => 'FFA0A0A0'
              ),
              'endcolor'   => array(
                'argb' => 'FFFFFFFF'
              )
            )
          )
      );
      
      $data = $this->get_data_export();
      if(is_array($data)){
        foreach ($data as $key => $value) {
          $objPHPExcel->getActiveSheet()->setCellValue('A'.(5+$key), $value->name);
          $objPHPExcel->getActiveSheet()->setCellValue('B'.(5+$key), $value->name_kategori);
          $objPHPExcel->getActiveSheet()->setCellValue('C'.(5+$key), $value->name_brand);
          $objPHPExcel->getActiveSheet()->setCellValue('D'.(5+$key), $value->kode);
          $objPHPExcel->getActiveSheet()->setCellValue('E'.(5+$key), $value->panjang);
          $objPHPExcel->getActiveSheet()->setCellValue('F'.(5+$key), $value->lebar);
          $objPHPExcel->getActiveSheet()->setCellValue('G'.(5+$key), $value->tinggi);
          $objPHPExcel->getActiveSheet()->setCellValue('H'.(5+$key), $value->massa);
          $objPHPExcel->getActiveSheet()->setCellValue('I'.(5+$key), $value->note);
        }
      }
      $objPHPExcel->getActiveSheet()->getStyle('A5:I'.(5+$key))->applyFromArray(
          array(
            'font'    => array(
              'bold'      => false
            ),
            'alignment' => array(
              'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
            ),
            'borders' => array(
              'bottom'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
              'left'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
              'right'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
            ),
            'fill' => array(
              'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
                'rotation'   => 90,
              'startcolor' => array(
                'argb' => 'FFA0A0A0'
              ),
              'endcolor'   => array(
                'argb' => 'FFFFFFFF'
              )
            )
          )
      );
      $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
      $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
       $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
         $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
          $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
           $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
           $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(30);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(50);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
      
      $objPHPExcel->setActiveSheetIndex(0);
      header('Content-Type: application/vnd.ms-excel');
      header('Content-Disposition: attachment;filename="'.$filename."-".date("Y m d").'"');
      header('Cache-Control: max-age=0');
      $objWriter = PHPExcel_IOFactory::createWriter($this->phpexcel, 'Excel5');
//$objWriter->save(str_replace('.php', '.xls', __FILE__));
      $objWriter->save('php://output');die;
    }
}
?>
